/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.craft.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.material.dao.model.MdMaterial;
import com.erp.prod.craft.dao.model.ProdWorkCraft;
import com.erp.prod.craft.dao.model.ProdWorkCraftCO;
import com.erp.prod.craft.service.ProdWorkCraftProcedureService;
import com.erp.prod.craft.service.ProdWorkCraftService;
import com.erp.prod.workcenter.service.ProdWorkCenterService;

@Controller
@RequestMapping("/web/prodWorkCraft")
public class ProdWorkCraftWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdWorkCraftWebController.class);
    
    //服务层注入
    @Autowired
    private ProdWorkCraftService prodWorkCraftService;
    @Autowired
    private ProdWorkCraftProcedureService prodWorkCraftProcedureService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private ProdWorkCenterService prodWorkCenterService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdWorkCraftList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-04 18:24:42
     * @author 
     * @param pages
     * @param prodWorkCraftCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCraftList")
    public String getProdWorkCraftList(Pages pages, ProdWorkCraftCO prodWorkCraftCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<ProdWorkCraft> prodWorkCraftList = this.prodWorkCraftService.getDataObjectsForDataAuth("", pages, prodWorkCraftCO);
        
        //获取物料
        Map materialMap = this.masterDataCommonService.getMaterialForMaterialMap();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodWorkCraftList", prodWorkCraftList);
        model.addAttribute("pages", pages);
        model.addAttribute("materialMap", materialMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=prodWorkCraft/prodWorkCraftList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-04 18:24:42
     * @author 
     * @param prodWorkCraft
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCraft")
    public String getProdWorkCraft(ProdWorkCraft prodWorkCraft, Model model) {
        //查询要编辑的数据
        if(prodWorkCraft!=null&&StringUtils.isNotBlank(prodWorkCraft.getCraftCode())) {
            ProdWorkCraft prodWorkCraftTemp = this.prodWorkCraftService.getDataObject(prodWorkCraft.getCraftCode());
            if(prodWorkCraftTemp!=null) {
                prodWorkCraft = prodWorkCraftTemp;
            }
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodWorkCraft.getMaterialCode());
            prodWorkCraft.setMaterialStandard(mdMaterial.getStandard());
        }
        
        //获取物料
        Map materialMap = this.masterDataCommonService.getMaterialForMaterialMap();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodWorkCraft", prodWorkCraft);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("materialMap", materialMap);
        
        return "basic.jsp?content=prodWorkCraft/prodWorkCraftEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-04 18:24:42
     * @author 
     * @param prodWorkCraft
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdWorkCraft")
    public String editProdWorkCraft(@Valid ProdWorkCraft prodWorkCraft, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getProdWorkCraft";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.prodWorkCraftService.insertOrUpdateDataObject(prodWorkCraft);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("craftCode", prodWorkCraft.getCraftCode());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "工艺编码已存在，请重新输入");
                return "redirect:getProdWorkCraftList";
            }else {
                throw e;
            }
        }
        
        return "redirect:getProdWorkCraft";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-04 18:24:42
     * @author 
     * @param prodWorkCraft
     * @return String
     *
     */
    @RequestMapping("deleteProdWorkCraft")
    public String deleteProdWorkCraft(ProdWorkCraft prodWorkCraft, RedirectAttributes attr) {
        //删除数据前验证数据
        if(prodWorkCraft!=null&&prodWorkCraft.getCraftId()!=null&&StringUtils.isNotBlank(prodWorkCraft.getCraftCode())) {
            int procedureCount = this.prodWorkCraftProcedureService.getProcedureCountByCraftCode(prodWorkCraft.getCraftCode());
            
            if(procedureCount==0) {
                //删除数据
                this.prodWorkCraftService.deleteDataObject(prodWorkCraft);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "工艺已关联工序，无法删除");
            }
        }
        
        return "redirect:getProdWorkCraftList";
    }
    
    
    
    /**
     * 
     * @description 更新审批状态
     * @date 2020年8月4日
     * @author dongbin
     * @param code
     * @param approveStatus
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("updateApproveStatus")
    public String updateApproveStatus(String code, String approveStatus, RedirectAttributes attr) {
        
        if(StringUtils.isNotBlank(code)&&StringUtils.isNotBlank(approveStatus)) {
            //更新审核状态
            this.prodWorkCraftService.updateApproveStatus(code, approveStatus);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("craftCode", code);
        }else {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "提交或审批数据错误");
            attr.addAttribute("craftCode", code);
        }
        
        return "redirect:getProdWorkCraft";
    }
}
